<?php
    $panngan2 = $_REQUEST["panngan2"];
    $panngan3 = $_REQUEST["panngan3"];
    $type_debt = $_REQUEST["type_debt"];
    $month = $_REQUEST["month"];
    $year = $_REQUEST["year"];
    
    if (is_null($type_debt) || !isset($type_debt) || empty($type_debt)) {
        
        $type_debt = "tax";
    }
    
    $ary_type_debt = array("tax" => "ภาษี", 
        "cremation1" => "ก.ฌ.", 
        "welfare" => "สวัสดิการ", 
        "cooperative" => "สหกรณ์", 
        "loan" => "กู้เทศบาล", 
        "bongkum" => "ธอ/บงคำ", 
        "ghb" => "ธอส", 
        "aia" => "AIA", 
        "cremation2" => "ก.ฌ. เทศบาล", 
        "other2" => "อื่นๆ");
?>
<script type="text/javascript">
	$(document).ready(function(){
		$('#petition').dataTable();
                    $( "#panngan2" ).change(function() {
			var panngan2= $(this).val();
			var panngan3 = "<?php echo $panngan3; ?>";
			
			//alert(panngan2);
			
			if (panngan2 != "") {
				//alert(panngan2);
				$.post('dd_panngan.php', {  panngan2: panngan2, panngan3: panngan3}, function(data) { 
					$("#dd_panngan3").html(data); 

				} );
			}

		});
                $('#panngan2').trigger('change');
	});
	function printCheque(month, year){
		var val = [];
		var count=0;
                            var type_debt = "<?php echo $type_debt; ?>";;
                            var panngan2 = "<?php echo $panngan2; ?>";
                            var panngan3 = "<?php echo $panngan3; ?>";
                            
		
		$(':checkbox:checked').each(function(i){
		  val[i] = $(this).val();
		  count++;
		  //alert(val[i]);
		});

		if (count == 0) {
			alert ("กรุณาเลือกข้อมูลที่ต้องการพิมพ์ด้วยครับ");
		} else {
			$('<div>').dialog2({
				autoOpen: true,
				title: "พิมพ์เช็ค",
				content: "dlg_prt_cheque_debt.php?val=" + val + "&month=" + month + "&year=" + year + "&type_debt=" + type_debt + "&panngan2=" + panngan2 + "&panngan3=" +panngan3,
				id: "modal-printCheque"
			});
		}
	}
</script>


<form method="get" action="index.php" >
	<input type="hidden" name="pages" value="<?php echo $pages; ?>">
        <div class="row-fluid">
                                <div class="control-group span4">
			<label class="control-label" for="panngan">แผนงาน</label>
			<div class="controls ">
				<select class="span12" id="panngan2" name="panngan2" required>
					<option value="">เลือก...</option>
				<?php


					$db = NEW myDBS;
					$db->selectDB("hr_udoncity");
					
					$sql = "SELECT DISTINCT panngan2_id 
								FROM position 
								WHERE lvl_id like '" .$_SESSION["lvl1_id"]. "%'
								AND panngan2_id IS NOT NULL"
								; 

					//echo $sql;

					$result = $db->runSQL($sql);
					
					while($rows = $db->fetch_object ( $result )){
						$db->selectDB("account");
						$sql = "SELECT DISTINCT * FROM panngan2 WHERE panngan2_id = '" .$rows->panngan2_id. "'";
						$result1 = $db->runSQL($sql);
						while($rows1 = $db->fetch_object ( $result1 )){
							if ($rows1->panngan2_id == $panngan2) {
								$selected = " selected";

							} else {
								$selected = "";
							}
							echo "<option value='".$rows1->panngan2_id."' $selected>$rows1->panngan2_name</option>";
						}
					}

				?>
				</select>
			</div>
		</div><!--/span-->

		<div class="control-group span4">
			<label class="control-label" for="panngan3">งาน</label>
			<div class="controls " id="dd_panngan3">
				<select class="span12" id="panngan3" name="panngan3">
					<option value="">เลือก</option>

				</select>
			</div>
		</div><!--/span-->  
                            <div class="control-group span4">
			<label class="control-label" for="ประเภท">ประเภทหนี้</label>
			<div class="controls " >
				<select class="span12" id="type_debt" name="type_debt">
					<option value="">เลือก</option>
                                        <option value="tax" <?php echo ($type_debt == "tax")? "selected":""; ?>>ภาษี</option>
                                        <option value="cremation1" <?php echo ($type_debt == "cremation1")? "selected":""; ?>>ก.ฌ.</option>
                                        <option value="welfare" <?php echo ($type_debt == "welfare")? "selected":""; ?>>สวัสดิการ</option>
                                        <option value="cooperative" <?php echo ($type_debt == "cooperative")? "selected":""; ?>>สหกรณ์</option>
                                        <option value="loan" <?php echo ($type_debt == "loan")? "selected":""; ?>>กู้เทศบาล</option>
                                        <option value="bongkum" <?php echo ($type_debt == "bongkum")? "selected":""; ?>>ธอ/บงคำ</option>
                                        <option value="ghb" <?php echo ($type_debt == "ghb")? "selected":""; ?>>ธอส</option>
                                        <option value="aia" <?php echo ($type_debt == "aia")? "selected":""; ?>>AIA</option>
                                        <option value="cremation2" <?php echo ($type_debt == "cremation2")? "selected":""; ?>>ก.ฌ. เทศบาล</option>
                                        <option value="other2" <?php echo ($type_debt == "other2")? "selected":""; ?>>อื่นๆ</option>
                                        

				</select>
			</div>
		</div><!--/span-->  
</div>
                <div class="row-fluid form-inline">
	<select name="month">
		<?php
			$array_month = array("01" => "มกราคม", "02" => "กุมภาพันธ์", "03" => "มีนาคม", "04" => "เมษายน", "05" => "พฤษภาคม", "06" => "มิถุนายน", "07" => "กรกฎาคม", "08" => "สิงหาคม", "09" => "กันยายน", "10" => "ตุลาคม", "11" => "พฤศจิกายน", "12" => "ธันวาคม");
			
			foreach ($array_month as $key => $value) {
				if ($key == $month) {
					echo "<option value='$key' selected>$value</option>";
				} else {
					echo "<option value='$key'>$value</option>";
				}
			}
		
		?>
	</select>
	<select name="year">
		<?php
			$y = date("Y");

			if (is_null($year) || !isset($year) || empty($year)) {
				$year = date("Y");
			}
			for ($i = $y - 5; $i < $y + 5; $i++ ) {
				if ($i == $year) {
					echo "<option value='$i' selected>$i</option>";
				} else {
					echo "<option value='$i'>$i</option>";
				}
			}
		?>
		
	</select>
	<input type="submit" class = "btn btn-info" value="ตกลง">
                </div>
</form>

<?php
	


        $sql = "
                SELECT card_id,  tax ,  other2 ,  cremation2 ,  aia ,  ghb ,  bongkum ,  loan ,  cooperative ,  welfare ,  cremation1, tbl_init.status 
                FROM  `tbl_init` , tbl_salary
                WHERE  `card_id` = id
                AND tbl_init.lvl1_id = '" .$_SESSION["lvl1_id"]. "'
                AND approved_status =  'อนุมัติ'
                AND tbl_init.panngan2_id =  '$panngan2'
                AND tbl_init.panngan3_id =  '$panngan3'
                and tbl_init.month_year = '$year-$month'
                ";
                //and month_year = '$year-$month'
	
	//$db = new myDBS;
	$db->selectDB("account");
                //echo $sql;
	$result = $db->runSQL($sql);

?>
<table id="petition" class="table table-striped table-bordered" width="100%" >
	<caption><br></caption>
	<thead>
		<tr>
			<th>#</th>
			<th>เลขที่บัตรประชาชน</th>
			<th>ชื่อ - นามสกุล</th>
			<th>ประเภทหนี้</th>
			<th>จำนวนเงิน</th>
			<th>เลือก</th>

		</tr>
	</thead>
	<tbody>
	<?php
		$i=0;
                                $db1 = new myDBS;
                                $db1->selectDB("hr_udoncity");
		while ( $rows = $db->fetch_object ( $result )){
			$i++;
                                            $sql ="SELECT fname, lname
						FROM person
						WHERE id = '". $rows->card_id ."'
						";

				
				$result1 = $db1->runSQL($sql);
				$rows1=$db1->fetch_object ( $result1 );

			echo "<tr>
                                                        <td>$i</td>
                                                        <td >$rows->card_id</td>
                                                        <td>$rows1->fname&nbsp;&nbsp;&nbsp;$rows1->lname</td>
                                                        <td>".$ary_type_debt[$type_debt]."</td>
                                                        <td>". number_format($rows->{$type_debt} , 2)."</td>
                                                        
			";
                                                            if (strpos($rows->status, $type_debt) <> false) {
					  echo "<td>พิมพ์แล้ว</td>";
				  } else {
					echo "<td><input type='checkbox' name='check[]' id='check_$rows->card_id' value='$rows->card_id'></td>";
				  }			

			echo "
			</tr>
			";

		}
	?>
	</tbody>
</table>
<a href="javascript:printCheque('<?php echo $month; ?>', '<?php echo $year; ?>')" role="button" class="btn btn-primary" target="_blank"><i class="icon-ok icon-white"></i> ตกลง</a>
<a href="index.php?pages=print_cheque_debt" role="button" class="btn " ><i class="icon-arrow-left"></i> กลับไป</a>